<template>
  <div class="mod-config">
    <el-form inline>
      <el-form-item label="数据源">
        <el-select v-model="dataForm.databaseName" placeholder="请选择数据源" clearable @change="databaseNameChangeHandle">
          <el-option v-for="item in dataSourceList" :key="item.id" :label="item.name" :value="item.name" />
        </el-select>
      </el-form-item>
      <el-form-item label="数据表">
        <el-select v-model="dataForm.tableName" placeholder="请选择表名" clearable @change="getFieldList">
          <el-option v-for="item in tableList" :key="item.tableName" :label="item.tableName" :value="item.tableName" />
        </el-select>
      </el-form-item>
    </el-form>
    <el-table :data="checkList" border v-if="fieldList.length">
      <el-table-column prop="tableField" label="表字段">
        <template slot-scope="scope">
          <el-select v-model="scope.row.tableField">
            <el-option v-for="item in fieldList" :value="item.columnName" :label="item.columnName" :key="item.columnName"></el-option>
          </el-select>
        </template>
      </el-table-column>
      <el-table-column prop="regex" label="正则表达式">
        <template slot-scope="scope">
          <el-select v-model="scope.row.id" @change="regexChangeHandle($event,scope.$index)">
            <el-option v-for="item in regexList" :value="item.id" :label="item.regexName" :key="item.id"></el-option>
          </el-select>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot="header" slot-scope="scope">
          <el-button type="primary" @click="addNewRegex">新增</el-button>
        </template>
        <template slot-scope="scope">
          <el-button type="danger" @click="delHandle(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <div style="margin-top: 15px;">
      <el-button type="primary" size="medium" icon="el-icon-search" @click="searchHandle">查询</el-button>
    </div>

    <div class="avue-crud" style="margin-top: 15px;">
      <el-table :data="dataList" border v-loading="tableLoading" :height="tableHeight">
        <template v-for="item in fieldList">
          <el-table-column :prop="item.columnName" :label="item.columnName"></el-table-column>
        </template>
      </el-table>
    </div>

    <div class="avue-crud__pagination">
      <el-pagination
        @size-change="sizeChangeHandle"
        @current-change="currentChangeHandle"
        :current-page="pageIndex"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="pageSize"
        :total="totalPage"
        background
        layout="total, sizes, prev, pager, next">
      </el-pagination>
    </div>

  </div>
</template>

<script>
  import { mapGetters } from 'vuex'
  import { filterParams } from '@/util/tools'
  import { fetchSelectDsList } from '@/api/gen/gen'
  import {
    fetchRegexInfoList,
    getRegexList,
    delRegexInfoObj,
    getListName,
    getListField,
    getRegexMessage
  } from '@/api/eims/regexinfo'

  export default {
    data() {
      return {
        dataForm: {
          key: ''
        },
        dataSourceList: [],
        tableList: [],
        fieldList: [],
        regexList: [],
        checkList: [],
        dataList: [],
        pageIndex: 1,
        pageSize: 50,
        totalPage: 0,
        tableLoading: false
      }
    },
    created() {
      // this.getDataList()
      this.getRegexList()
      this.getdataSourceList()
    },
    computed: {
      ...mapGetters(['permissions']),
      tableHeight: function(){
        return window.innerHeight - 260
      }
    },
    methods: {
      getRegexList(){
        getRegexList().then(response => {
          if(response.data.code == '0'){
            this.regexList = response.data.data
          }else{
            this.$message.error(response.data.msg)
          }
        })
      },
      getFieldList(){
        getListField(this.dataForm.databaseName,this.dataForm.tableName).then(response => {
          if(response.data.code == '0'){
            this.fieldList = response.data.data
          }else{
            this.$message.error(response.data.msg)
          }
        })
      },
      // 添加新的字段正则表达式
      addNewRegex(){
        let obj = {
          stamp: (new Date()).valueOf(),
          tableField: '',
          regex: ''
        }
        this.checkList.push(obj)
      },
      // 删除
      delHandle(data) {
        // console.log(data)
        this.checkList.forEach((item,index) => {
          if(item.stamp == data.stamp){
            this.checkList.splice(index,1)
          }
        })
      },
      getdataSourceList() {
        fetchSelectDsList().then(response => {
          if(response.data.code == '0'){
            this.dataSourceList = response.data.data
          }else{
            this.$message.error(response.data.msg)
          }
        })
      },
      // 数据源改变事件
      databaseNameChangeHandle(val){
        getListName(val).then(response => {
          if(response.data.code == '0'){
            this.tableList = response.data.data
          }else{
            this.$message.error(response.data.msg)
          }
        })
      },
      regexChangeHandle($event,index){
        let obj = {}
        obj = this.regexList.find(item => {
          if(item.id == $event){
            return item
          }
        })
        this.checkList[index].regexName = obj.regexName
        this.checkList[index].regex = obj.regex
      },
      searchHandle(){
        let form = {
          current: this.pageIndex,
          size: this.pageSize,
          pageName: this.dataForm.databaseName,
          className: this.dataForm.tableName,
          checks: this.checkList,
        }

        getRegexMessage(form).then(response => {
          if(response.data.code == '0'){
            this.dataList = response.data.data
          }else{
            this.$message.error(response.data.msg)
          }
        })
      },
      sizeChangeHandle(val) {
        this.pageSize = val
        this.pageIndex = 1
        this.searchHandle()
      },
      currentChangeHandle(val) {
        this.pageIndex = val
        this.searchHandle()
      },
    }
  }
</script>
